package cn.orangelite.ai.controller;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import cn.orangelite.common.core.constant.Constants;
import cn.orangelite.common.core.utils.DateUtils;
import cn.orangelite.common.core.utils.StringUtils;
import cn.orangelite.common.core.utils.file.FileUtils;
import cn.orangelite.common.security.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import cn.orangelite.common.log.annotation.Log;
import cn.orangelite.common.log.enums.BusinessType;
import cn.orangelite.common.security.annotation.RequiresPermissions;
import cn.orangelite.ai.domain.AiPlugins;
import cn.orangelite.ai.service.IAiPluginsService;
import cn.orangelite.common.core.web.controller.BaseController;
import cn.orangelite.common.core.web.domain.AjaxResult;
import cn.orangelite.common.core.utils.poi.ExcelUtil;
import cn.orangelite.common.core.web.page.TableDataInfo;

/**
 * 插件列Controller
 * 
 * @author dongfaqing
 * @date 2024-01-18
 */
@RestController
@RequestMapping("/plugins")
public class AiPluginsController extends BaseController
{
    @Autowired
    private IAiPluginsService aiPluginsService;

    /**
     * 查询插件列列表
     */
    @RequiresPermissions("ai:plugins:list")
    @GetMapping("/list")
    public TableDataInfo list(AiPlugins aiPlugins)
    {
        startPage();
        List<AiPlugins> list = aiPluginsService.selectAiPluginsList(aiPlugins);
        return getDataTable(list);
    }

    /**
     * 导出插件列列表
     */
    @RequiresPermissions("ai:plugins:export")
    @Log(title = "插件列", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AiPlugins aiPlugins)
    {
        List<AiPlugins> list = aiPluginsService.selectAiPluginsList(aiPlugins);
        ExcelUtil<AiPlugins> util = new ExcelUtil<AiPlugins>(AiPlugins.class);
        util.exportExcel(response, list, "插件列数据");
    }

    /**
     * 获取插件列详细信息
     */
    @RequiresPermissions("ai:plugins:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id)
    {
        return success(aiPluginsService.selectAiPluginsById(id));
    }

    /**
     * 新增插件列
     */
    @RequiresPermissions("ai:plugins:add")
    @Log(title = "插件列", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AiPlugins aiPlugins)
    {
        aiPlugins.setUserId(SecurityUtils.getUserId());
        aiPlugins.setCreateTime(DateUtils.getNowDate());
        return toAjax(aiPluginsService.insertAiPlugins(aiPlugins));
    }

    /**
     * 修改插件列
     */
    @RequiresPermissions("ai:plugins:edit")
    @Log(title = "插件列", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AiPlugins aiPlugins)
    {
        aiPlugins.setCreateTime(DateUtils.getNowDate());
        return toAjax(aiPluginsService.updateAiPlugins(aiPlugins));
    }

    /**
     * 删除插件列
     */
    @RequiresPermissions("ai:plugins:remove")
    @Log(title = "插件列", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids)
    {
        return toAjax(aiPluginsService.deleteAiPluginsByIds(ids));
    }


    @GetMapping("/market")
    public TableDataInfo getMarketPluginsList(AiPlugins aiPlugins){
        startPage();
        return getDataTable(aiPluginsService.list(Wrappers.<AiPlugins>lambdaQuery().like(StringUtils.isNotEmpty(aiPlugins.getName()),AiPlugins::getName,aiPlugins.getName())));
    }

}
